package com.yifi.common.base;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.yifi.common.model.AjaxResult;
import com.yifi.common.utils.JWTUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.common.utils.ToolString;
import com.yifi.common.vo.TableDataInfo;

import java.util.List;

/**
 * @author 姚善利
 * @classname com.yifi.common.base
 * @description
 * @date 2022/7/21 10:15
 */
public class BaseService {

	public String getLoginUser(Controller controller,boolean ismobile)
	{
		String username="";
		if(ismobile)
		{
			String token = controller.getRequest().getHeader("Authorization");
			username= JWTUtils.getUserByCol(token, "cname");
		}
		else
		{
			username= ToolSession.getUser(controller).getCname();
		}

		return username;
	}

	public String getToken(Controller controller)
	{
		return controller.getRequest().getHeader("Authorization");
	}
	/**
	 *      获取单行值
	 * @param table
	 * @param key
	 * @param value
	 * @return
	 */
	public Record getData(String table,String key,String value)
	{
		String sql="select * from "+table+" where "+key+"='"+value+"' ";
		return Db.findFirst(sql);
	}

	/**
	 * 根据关键字段删除记录
	 * @param table
	 * @param key
	 * @param value
	 * @return
	 */
	public AjaxResult remove(String table, String key,String value)
	{
		String sql="delete from "+table+" where "+key+"='"+value+"' ";
		if(Db.update(sql)>0)
		{
			return AjaxResult.success("删除成功！");
		}
		else
		{
			return AjaxResult.error("删除失败！");
		}
	}

	/**
	 *  根据关键字段的 字段组删除记录
	 * @param table
	 * @param key
	 * @param value
	 * @return
	 */
	public AjaxResult removes(String table, String key,String value)
	{
		value= ToolString.getIdsToStr(value);
		String sql="delete from "+table+" where "+key+" in ("+value+")";
		if(Db.update(sql)>0)
		{
			return AjaxResult.success("删除成功！");
		}
		else
		{
			return AjaxResult.error("删除失败！");
		}
	}

	/**
	 *  组合分页信息
	 * @param pageNum
	 * @param pageSize
	 * @param sqlSelect
	 * @param sqlExcept
	 * @param ispage
	 * @return
	 */
	public TableDataInfo dataPage(int pageNum,int pageSize,String sqlSelect,String sqlExcept,boolean ispage)
	{
		TableDataInfo rspData = new TableDataInfo();
		if(ispage)
		{
			Page<Record> pageR = Db.paginate(pageNum, pageSize, sqlSelect, sqlExcept);
			rspData.setCode(0);
			rspData.setRows(pageR.getList());
			rspData.setTotal(pageR.getTotalRow());
			rspData.setTotalPage(pageR.getTotalPage());
		}
		else
		{
			String sql=sqlSelect+sqlExcept;
			List<Record> listRec=Db.find(sql);
			rspData.setCode(0);
			rspData.setRows(listRec);
			rspData.setTotal(listRec.size());
			rspData.setTotalPage(1);
		}
		return rspData;
	}

	public AjaxResult success(String msg)
	{
		return AjaxResult.success(msg);
	}

	public AjaxResult success(String msg,Object obj)
	{
		return AjaxResult.success(msg,obj);
	}

	public AjaxResult error(String msg)
	{
		return AjaxResult.error(msg);
	}
}
